package com.wxy.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

/**
 * @author 47868
 * spring security的配置
 */
@Configuration
@EnableMethodSecurity
public class DetailsConfig {
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        return http
                //.csrf().disable()
                .authorizeHttpRequests(auth->{
                    auth.requestMatchers("/static/**","/springboot1/**","/mq/**","/error","login","/register","/security/register","/rtest/**",
                           "/img/**","/file/**", "/swagger-ui.html", "/swagger-ui/**", "/v3/api-docs","/test/**").permitAll();
                    auth.requestMatchers("/security/hello").authenticated();
                    auth.requestMatchers("/favicon.ico").permitAll();
                    auth.anyRequest().authenticated();
                })
                .formLogin(conf->{
                    conf.loginPage("/security/login");
                    conf.loginProcessingUrl("/security/doLogin");
                    conf.defaultSuccessUrl("/security/hello");
                    conf.permitAll();
                })
                .logout(conf->{
                    conf.logoutUrl("/security/logout");
                    conf.permitAll();
                }).build();
    }
}